package com.lu.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.lu.core.annotations.SystemLog;
import com.lu.model.domain.SystemMethodLog;
import com.lu.model.entity.SysMerchant;
import com.lu.model.request.MerchantAddRequest;
import com.lu.model.request.MerchantQueryRequest;
import com.lu.model.request.ProcessApprovalUpdateInfoRequest;
import com.lu.model.response.Result;
import com.lu.service.SysApprovalService;
import com.lu.service.SysMechantTypesService;
import com.lu.service.SysMerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商户 前端控制器
 * </p>
 *
 * @author zhanglu
 * @since 2022-07-31
 */
@RestController
@RequestMapping("/sys-merchant")
@RequiredArgsConstructor
public class SysMerchantController {

  private final SysMerchantService merchantService;
  private final SysApprovalService approvalService;
  private final SysMechantTypesService mechantTypesService;

  @PreAuthorize("hasAuthority('/sys-merchant/page-list')")
  @PostMapping("/page-list")
  public Result<PageInfo<SysMerchant>> pageList(@RequestBody MerchantQueryRequest request){
      return Result.success(merchantService.pageList(request));
  }

  @SystemLog(code = SystemMethodLog.Code.CAKE_EVOUCHERS_ORDER_CREATE)
  @PostMapping("/list")
  public Result<List<SysMerchant>> list(@RequestBody MerchantQueryRequest request){
    return Result.success(merchantService.merchantList(request));
  }

  @PreAuthorize("hasAuthority('/sys-merchant/get')")
  @GetMapping("/get/{id}")
  public Result<SysMerchant> get(@PathVariable Long id){
    SysMerchant sysMerchant = merchantService.getById(id);
    if(sysMerchant != null){
      sysMerchant.setMechantTypes(mechantTypesService.getById(sysMerchant.getMerchantTypesId()));
    }
    return Result.success(sysMerchant);
  }

  @PreAuthorize("hasAuthority('/sys-merchant/add')")
  @PostMapping("/add")
  public Result<Long> add(@RequestBody @Validated MerchantAddRequest request){
    return Result.success(merchantService.addMerchant(request));
  }

  /**
   * 审批修改 - 修改审核信息，将status设置为 审核中，即重新发起审核流程
   *  - 只有如下场景才可以修改
   *  - @see {@link com.lu.model.enums.ApprovalStatusEnum#UN_AUDITED} {@link com.lu.model.enums.ApprovalStatusEnum#NO_PASS}
   * @param merchant
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-merchant/update-approval')")
  @PostMapping("/update-approval")
  public Result<SysMerchant> updateApproval(@RequestBody SysMerchant merchant){
    ProcessApprovalUpdateInfoRequest request = new ProcessApprovalUpdateInfoRequest();
    request.setTableName("sys_merchant");
    request.setObjId(merchant.getId());
    merchant.setId(null);
    merchant.setCreateUserId(null);
    merchant.setCreateTime(null);
    merchant.setStatus(null);
    merchant.setStoreCount(null);
    merchant.setBrandName(null);
    merchant.setUsername(null);
    merchant.setUndeliveredOrderCount(null);
    merchant.setMechantTypes(null);
    request.setParams(JSONUtil.parseObj(merchant));
    return Result.success(approvalService.updateInfo(request));
  }

  @PreAuthorize("hasAuthority('/sys-merchant/update')")
  @PostMapping("/update")
  public Result<SysMerchant> update(@RequestBody SysMerchant merchant){
    return Result.success(merchantService.updateMerchant(merchant));
  }

  @PreAuthorize("hasAuthority('/sys-merchant/delete')")
  @PostMapping("/delete/{id}")
  public Result<String> delete(@PathVariable Long id){
    return Result.success(merchantService.delete(id));
  }

}
